package com.itany.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.constant.ResponseConstant;
import com.itany.exception.*;
import com.itany.pojo.User;
import com.itany.user.service.IUserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping("/userserver")
public class UserController {

    @Reference
    private IUserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;



    @RequestMapping("/findUsers")
    public ModelAndView findUsers(@RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.findUsers(pageNo, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageInfo", pageInfo);
        return new ModelAndView("userlist", map);
    }

    @RequestMapping("/login")
    @ResponseBody
    public ActionResult login(String phone,String password, HttpServletResponse response) {
        User u;
        ActionResult actionResult = new ActionResult();
        try {
            User user = new User();
            user.setPhone(phone);
            user.setPassword(password);
            u = userService.login(user);
            //生成一个redis的唯一key
            String token = UUID.randomUUID().toString();
            //向redis中存入用户信息
            redisTemplate.opsForValue().set("USER_TOKEN::" + token, JSON.toJSONString(u), 1800);
            //将key存入cookie
            Cookie ck = new Cookie("TT_TOKEN", token);
            ck.setPath("/");
            response.addCookie(ck);
            actionResult.setStatus(ResponseConstant.RESPONSE_SUCCESS);
            actionResult.setMsg("登录成功");
        } catch (RequestParamsException |WrongUsernameOrPasswordException e) {
            actionResult.setMsg(e.getMessage());
           actionResult.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            actionResult.setMsg("服务器内部异常，请稍后再试");
           actionResult.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return actionResult;

    }

    @RequestMapping("/register")
    @ResponseBody
    public ActionResult register(String phone, String password, String repassword, String phonecode) {
        ActionResult result = new ActionResult();
        try {
            userService.register(phone, password, repassword, phonecode);
            result.setMsg("注册成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (InconsistentPasswordsException | PhoneCodeErrorException | DuplicatePhoneException | MalformedPhoneException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        }catch (Exception e){
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    @RequestMapping("/getphonecode")
    @ResponseBody
    public ActionResult getphonecode(String phone) {
        return userService.getphonecode(phone);
    }


}
